#include"Stack.h"
void STInit(Stack* ps) {
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}
void STDestroy(Stack* ps) {
	assert(ps);
	if (ps->arr) {
		free(ps->arr);
		ps->arr = NULL;
	}
	ps->capacity = ps->size = 0;
}
bool STIsEmpty(Stack* ps){
	assert(ps);
	return ps->size == 0;
}
void STPush(Stack* ps, STDataType val) {
	assert(ps);
	if (ps->capacity == ps->size) {
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->arr, sizeof(STDataType) * newCapacity);
		if (tmp == NULL) {
			perror("realloc fail");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	ps->arr[ps->size++]=val;
}
void STPop(Stack* ps) {
	assert(ps);
	assert(!STIsEmpty(ps));
	ps->size--;
}
STDataType STTop(Stack* ps) {
	assert(ps);
	assert(!STIsEmpty(ps));
	return ps->arr[ps->size - 1];
}
int STSize(Stack* ps) {
	assert(ps);
	return ps->size;
}